package com.shengdong.water.code.viewmodel

import com.shengdong.water.base.BaseViewModel
import com.shengdong.water.code.bean.Outlet
import com.shengdong.water.code.bean.OutletCategoryBean
import com.shengdong.water.code.bean.RealTimeBean
import com.shengdong.water.code.repository.ApiRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject
import kotlin.collections.plus

@HiltViewModel
class InspectViewModel @Inject constructor(
    private val repository: ApiRepository
) : BaseViewModel()  {

    private var isRefreshing = false
    private val _realTimeList = MutableStateFlow<List<RealTimeBean>>(emptyList())
    val realTimeList: StateFlow<List<RealTimeBean>> = _realTimeList

    private val _outlets = MutableStateFlow<MutableList<Outlet>>(mutableListOf())
    val outlets: StateFlow<MutableList<Outlet>> = _outlets

    private val _category = MutableStateFlow<MutableList<OutletCategoryBean>>(mutableListOf())
    val category: StateFlow<MutableList<OutletCategoryBean>> = _category


//    init {
//        getRealTimeList(1,10)
//    }
    fun getRealTimeList( queryMap: Map<String, Any>) {

        handlePagingRequest(
            showLoading = true,
            loading = "加载中...",
            flow = repository.getRealTimeList(queryMap),
            onSuccess ={ it ->
                _realTimeList.update { oldList ->
                    if (isRefreshing || queryMap["current"] == 1) {
                        isRefreshing = false
                        it
                    } else {
                        oldList + it.filterNot { newItem ->
                            oldList.any { it.id == newItem.id }
                        }
                    }

                }
            },
            onError = { code, msg ->
                when (code) {
                    401 -> {}// 处理未授权
                    403 -> {}// 处理禁止访
                    else -> showError(msg)
                }
            },
            onException = { e ->
                showError("发生错误: ${e.message}")
            },
        )
    }






    fun getAllOutlets() {
        handleRequest(
            showLoading = false,
            flow = repository.getAllOutlets(),
            onSuccess = { _outlets.value = it },
            onError = { code, msg ->
                when (code) {
                    401 -> {}// 处理未授权
                    403 -> {}// 处理禁止访问
                    else -> showError(msg)
                }
            },
            onToast =  {
                it
            },
            onException = { e ->
                showError("发生错误: ${e.message}")
            },
        )
    }

    fun getOutletCategory() {
        handleRequest(
            showLoading = false,
            flow = repository.getOutletCategory(),
            onSuccess = {
                _category.value = it
            },
            onToast =  {
                it
            },
            onError = { code, msg ->
                when (code) {
                    401 -> {}// 处理未授权
                    403 -> {}// 处理禁止访问
                    else -> showError(msg)
                }
            },
            onException = { e ->
                showError("发生错误: ${e.message}")
            },
        )
    }


}




